const app = getApp()
const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
Page({
  data: {
    discountCard: { "title": "", 'price': '', "startTime": '', "endTime": '', "minMoney": '',"astrict": 1},//新建优惠券
    discountCardList:[],//优惠券列表
    //辅助
    auxiliary: { "nowDate": '', 'showAddCard': false, 'showDelect':false, 'endTimeStart': '','updateIndex':''},
  },
  onLoad: function (options) {
    var _this = this;
    _this.setData({
      ["auxiliary.nowDate"]: util.getDateStr(new Date(), 0)
    })
  },
  /**
  * 打开添加优惠券面板
  */
  openAddCard: function () {
    var _this = this;
    _this.setData({
      ["auxiliary.showAddCard"]: true
    })
  },
  /**
  * 打开删除面板
  */
  openDelect: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    _this.setData({
      ["auxiliary.updateIndex"]: index,
      ["auxiliary.showDelect"]: true,
    })
  },
  /**
   * 关闭所有弹窗
   */
  closeMC: function () {
    var _this = this;
    _this.setData({
      ["auxiliary.showAddCard"]: false,
      ["auxiliary.showDelect"]: false,
    })
  },
  /**
   * 设置优惠券名称
   */
  inputCardTitle: function(e){
    var _this = this;
    var value = e.detail.value.replace(/\s+/g, '');
    _this.setData({
      ["discountCard.title"]: value,
    })
  },
  /**
   * 设置优惠券金额
   */
  inputCardPrice: function (e) {
    var _this = this;
    var value = parseInt(e.detail.value.replace(/\s+/g, ''));
    _this.setData({
      ["discountCard.price"]: value,
    })
  },
  /**
   * 设置优惠券最低消费
   */
  inputCardMinMoney: function (e) {
    var _this = this;
    var price = _this.data.discountCard.price;
    var value = parseInt(e.detail.value.replace(/\s+/g, ''));
    _this.setData({
      ["discountCard.minMoney"]: value,
    })
  },
  /**
   * 验证优惠券最低消费
   */
  checkedMinMoney: function () {
    var _this = this;
    if (_this.data.discountCard.minMoney != '' && _this.data.discountCard.minMoney!=null){
      if (_this.data.discountCard.price + 1 > _this.data.discountCard.minMoney) {
        wx.showToast({
          title: '满减要高于优惠',
          icon: 'none',
          duration: 1500
        })
        _this.setData({
          ["discountCard.minMoney"]: '',
        })
      }  
    }
  },
  /**
   * 设置优惠券数量
   */
  inputCardAstrict: function (e) {
    var _this = this;
    var value = parseInt(e.detail.value.replace(/\s+/g, ''));
    _this.setData({
      ["discountCard.astrict"]: value,
    })
  },
   /**
   * 设置优惠券开始时间
   */
  selectStartTime: function(e) {
    var _this = this;
    var value = e.detail.value;
    var valueLast = util.getDateStr(value, 1)
    _this.setData({
      ["discountCard.startTime"]: value,
      ["auxiliary.endTimeStart"]: valueLast,
    })
    var endTime = _this.data.discountCard.endTime
    if (new Date(endTime.replace(/-/g, '/')).getTime() <= new Date(value.replace(/-/g, '/')).getTime()){
      _this.setData({
        ["discountCard.endTime"]: valueLast,
      })
    }
  },
  /**
  * 设置优惠券结束时间
  */
  selectEndTime: function (e) {
    var _this = this
    var value = e.detail.value;
    _this.setData({
      ["discountCard.endTime"]: value,
    })
  },
  /**
  * 删除优惠券
  */
  delectCard: function(){
    var _this = this
    var index = _this.data.auxiliary.updateIndex;
    var arr = _this.data.discountCardList;
    arr.splice(index, 1);
    _this.setData({
      discountCardList: arr,
    })
  },
  /**
  * 修改优惠券
  */
  updataCard: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index;
    var arr = _this.data.discountCardList
    _this.setData({
      discountCard: arr[index-1],
      ["auxiliary.updateIndex"]: index,
      ["auxiliary.showAddCard"]: true,
    })
  },
  /**
  * 保存优惠券
  */
  saveDiscountCard: function (e) {
    var _this = this
    var info = _this.data.discountCard;
    var arr = _this.data.discountCardList;
    var index = _this.data.auxiliary.updateIndex;
    if (index != '' && index!=null){
      arr[index-1] = info
      _this.setData({
        discountCardList: arr,
      })
      _this.setData({
        discountCard: { "title": "", 'price': '', "startTime": '', "endTime": '', "minMoney": '', "astrict": 1 },
        ["auxiliary.showAddCard"]: false,
        ["auxiliary.updateIndex"]: '',
      })
    }else{
      arr.push(info)
      _this.setData({
        discountCardList: arr,
      })
      _this.setData({
        discountCard: { "title": "", 'price': '', "startTime": '', "endTime": '', "minMoney": '', "astrict": 1 },
        ["auxiliary.showAddCard"]: false,
      })
    }
  },
})